package com.jiang.myfm.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jiang.myfm.R;
import com.jiang.myfm.activity.PlayActivity;
import com.jiang.myfm.adapters.BillboardInfoItemAdapter;
import com.jiang.myfm.entities.billboard.DataEntity;
import com.jiang.myfm.entities.billboard.DataList;
import com.jiang.myfm.utils.HttpService;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class BillboardInfoFragment extends Fragment implements Callback<DataEntity>, SwipeRefreshLayout.OnRefreshListener,BillboardInfoItemAdapter.onChildClickListener{


    private String contentType;
    private String key;
    private RecyclerView recycler;
    private BillboardInfoItemAdapter adapter;
    private SwipeRefreshLayout refreshLayout;
    private int pageId = 1;
    private List<DataList> datas;
    private LinearLayoutManager manager;
    private int lastVisibleItemPosition;
    private HttpService httpService;

    public static BillboardInfoFragment newInstance(String contentType, String key) {

        Bundle args = new Bundle();
        args.putString("contentType",contentType);
        args.putString("key",key);
        BillboardInfoFragment fragment = new BillboardInfoFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        contentType = bundle.getString("contentType");
        key = bundle.getString("key");

        datas = new ArrayList<DataList>();

        httpService = new Retrofit.Builder()
                .baseUrl("http://mobile.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(HttpService.class);
        httpService.getData(contentType,"android",key,pageId,20,2).enqueue(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_billboard_info, container, false);

        manager = new LinearLayoutManager(getContext());
        recycler = ((RecyclerView) view.findViewById(R.id.bif_recycler));
        recycler.setLayoutManager(manager);
        refreshLayout = ((SwipeRefreshLayout) view.findViewById(R.id.billboard_info_refresh));
        refreshLayout.setOnRefreshListener(this);
        adapter = new BillboardInfoItemAdapter(getContext());
        adapter.setListener(this);
        recycler.setAdapter(adapter);
        recycler.addOnScrollListener(new RecyclerView.OnScrollListener() {

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (lastVisibleItemPosition+1 == adapter.getItemCount() && newState == RecyclerView.SCROLL_STATE_IDLE){
                    httpService.getData(contentType,"android",key,pageId+1,20,2).enqueue(BillboardInfoFragment.this);
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItemPosition = manager.findLastVisibleItemPosition();
                boolean isSwipeEnable = false;
                if (adapter.getItemCount()>0){
                    boolean isFirst = manager.findFirstVisibleItemPosition() == 0;
                    boolean isTop = manager.getChildAt(0).getTop() == 0;
                    isSwipeEnable = isFirst && isTop;
                }
                refreshLayout.setEnabled(isSwipeEnable);
            }
        });
        return view;
    }


    @Override
    public void onResponse(Call<DataEntity> call, Response<DataEntity> response) {
        datas = response.body().getList();
        adapter.addAll(datas);

        refreshLayout.setRefreshing(false);
    }

    @Override
    public void onFailure(Call<DataEntity> call, Throwable t) {

    }

    @Override
    public void onRefresh() {
        datas.clear();
        pageId = 1;
        httpService.getData(contentType,"android",key,pageId,20,2).enqueue(this);
    }


    @Override
    public void onChildClick(String trackId,String uid) {
        Intent intent = new Intent(getActivity(), PlayActivity.class);
        intent.putExtra("trackId",trackId);
        intent.putExtra("uid",uid);
        startActivity(intent);
    }
}
